<script setup lang="ts">
import { useGuessList } from '@/composables'
import { useMemberStore } from '@/stores'
import { ref } from 'vue'
import { isworker,getuserinfo,getuserbalance } from '@/services/login'
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 订单选项

import { onShow } from "@dcloudio/uni-app";
  // 获取用户信息
  let user_data = ref<any>();
  let userinfo = ref<any>();
  let is_worker = ref<any>();
  let banlce = ref<any>();
  onShow(async() => {
    let wxUser = uni.getStorageSync("wxuser")
    if (!wxUser) {
      user_data.value.avatarUrl = "";
      user_data.value.nickName = "";
    } else {
      // user_data.value.avatarUrl = wxUser.avatarUrl;
      user_data.value =  wxUser;
    }
    const res:any = await getuserinfo({
      token: user_data.value.token
    })
    const ress:any = await getuserbalance({
      token: user_data.value.token
    })
    if(ress.success){
      banlce.value = ress.balance
    }

    // banlce.value = ress

    if(res.message == 'Invalid token'){
      uni.removeStorageSync("userinfo")
      uni.removeStorageSync("wxuser")

    }

    console.log(res,'res');
    if(res.username){
      uni.setStorageSync("userinfo",res)
      // uni.showToast({ title: "登陆成功", icon: "none", duration: 1000 });
    }else{
      uni.showToast({ title: "信息获取失败", icon: "none", duration: 1000 });
      uni.removeStorageSync("userinfo")
      uni.removeStorageSync("wxuser")
      setTimeout(() => {
        uni.navigateTo({
          url:'/pages/login/login'
        })
        uni.hideLoading();
      }, 600);
    }
    userinfo.value = res
    const resd:any = await isworker({
      user_id: userinfo.value.user_id
    })
    console.log(resd,'resd');
    is_worker.value = resd.is_worker
  })
function gores(){
  // uni.navigateTo({
  //   url:'/pages/rescueex/index'
  // })
  uni.navigateTo({
      url:'/pages/self-test/topic'
  })
}
// 获取会员信息
// const memberStore = useMemberStore()
// const orderTypes = [
//   { type: '1', text: '待付款', icon: 'icon-currency' },
//   { type: '2', text: '待发货', icon: 'icon-gift' },
//   { type: '3', text: '待收货', icon: 'icon-check' },
//   { type: '4', text: '待评价', icon: 'icon-comment' },
// ]
const { guessRef, onScrolltolower } = useGuessList()
// goUser() {
// 				uni.switchTab({
// 					url: '/pages/user/user'
// 				})
// 			},
// 			goAdmin() {
// 				uni.switchTab({
// 					url: '/pages/admin/admin'
// 				})
// 			}
const goUser = () =>{
  				uni.switchTab({
					url: '/pages/user/index'
		})
  }
const goAdmin = () =>{
  				uni.switchTab({
					url: '/pages/admin/index'
		})
  }
</script>

<template>
  <scroll-view enable-back-to-top @scrolltolower="onScrolltolower" class="viewport" scroll-y>
    <!-- 个人资料 -->
    <view class="profile" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
      <!-- 情况1：已登录 -->
      <view class="overview" v-if="userinfo">
        <navigator url="/pagesMember/profile/profile" hover-class="none">
          <image class="avatar" src="https://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png" mode="aspectFill"></image>
        </navigator>
        <view class="meta">
          <view class="nickname">
            {{ userinfo.username  }}
          </view>
          <view class="" v-if="banlce">
            余额：{{banlce}}
          </view>
        </view>
      </view>
      <!-- 情况2：未登录 -->
      <view class="overview" v-else>
        <navigator url="/pages/login/login" hover-class="none">
          <image
            class="avatar gray"
            mode="aspectFill"
            src="https://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png"
          ></image>
        </navigator>
        <view class="meta">
          <navigator url="/pages/login/login" hover-class="none" class="nickname">
            未登录
          </navigator>
          <view class="extra">
            <text class="tips">点击登录账号</text>
          </view>
        </view>
      </view>

    </view>
    <view class="" v-if="userinfo">
     <!-- 我的订单 -->
        <view class="orders"  v-if="!is_worker">

          <view class="section" @click="goUser">
            <!-- 订单 -->
            <p>用户</p>
          </view>

        </view>
        <view class="orders"  v-if="!is_worker">

          <view class="section" @click="gores">
            <!-- 订单 -->
            <p>注册工作人员</p>
          </view>
        </view>
        <view class="orders" v-else>

          <view class="section" @click="goAdmin">
            <!-- 订单 -->
            <p>工作人员</p>
          </view>
        </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
  background-color: #f7f7f8;
}

.viewport {
  height: 100%;
  background-repeat: no-repeat;
  background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/center_bg.png);
  background-size: 100% auto;
}

/* 用户信息 */
.profile {
  margin-top: 30rpx;
  position: relative;

  .overview {
    display: flex;
    height: 120rpx;
    padding: 0 36rpx;
    color: #fff;
  }

  .avatar {
    width: 120rpx;
    height: 120rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .gray {
    filter: grayscale(100%);
  }

  .meta {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    line-height: 30rpx;
    padding: 16rpx 0;
    margin-left: 20rpx;
  }

  .nickname {
    max-width: 180rpx;
    margin-bottom: 16rpx;
    font-size: 30rpx;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .extra {
    display: flex;
    font-size: 20rpx;
  }

  .tips {
    font-size: 22rpx;
  }

  .update {
    padding: 3rpx 10rpx 1rpx;
    color: rgba(255, 255, 255, 0.8);
    border: 1rpx solid rgba(255, 255, 255, 0.8);
    margin-right: 10rpx;
    border-radius: 30rpx;
  }

  .settings {
    position: absolute;
    bottom: 0;
    right: 40rpx;
    font-size: 30rpx;
    color: #fff;
  }
}

/* 我的订单 */
.orders {
  position: relative;
  z-index: 99;
  padding: 30rpx;
  margin: 61rpx 95rpx 0;
  background-color: #fff;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);
  height: 192rpx;

  .title {
    height: 40rpx;
    line-height: 40rpx;
    font-size: 28rpx;
    color: #1e1e1e;

    .navigator {
      font-size: 24rpx;
      color: #939393;
      float: right;
    }
  }

  .section {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 40rpx 20rpx 10rpx;
    justify-content: center;
    p{
      font-size: 16px;
    }
    .navigator,
    .contact {
      text-align: center;
      font-size: 24rpx;
      color: #333;
      &::before {
        display: block;
        font-size: 60rpx;
        color: #ff9545;
      }
      &::after {
        border: none;
      }
    }
    .contact {
      padding: 0;
      margin: 0;
      border: 0;
      background-color: transparent;
      line-height: inherit;
    }
  }
}

/* 猜你喜欢 */
.guess {
  background-color: #f7f7f8;
  margin-top: 20rpx;
}
</style>
